﻿/// <reference path="//Microsoft.WinJS.1.0/js/base.js" />
var readFile = function (existingFile) {
    Windows.Storage.FileIO.readTextAsync(existingFile).done(function (fileContent) {
        var primesPlaceholder = document.getElementById("result");
        primesPlaceholder.innerText = fileContent;
    })
}

var saveFileFirstNPrimes = function (primesCalculator, localFolder, fileName, value) {
    localFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.failIfExists).then(function (createdFile) {
        primesCalculator.calculateFirstNPrimes(value).then(function (result) {
            Windows.Storage.FileIO.writeTextAsync(createdFile, result.join(", "));

            var primesPlaceholder = document.getElementById("result");
            primesPlaceholder.innerText = result.join(", ");
        });
    });
}

var saveFileToNPrimes = function (primesCalculator, localFolder, fileName, value) {
    localFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.failIfExists).then(function (createdFile) {
        primesCalculator.calculateToNPrimes(value).then(function (result) {
            Windows.Storage.FileIO.writeTextAsync(createdFile, result.join(", "));

            var primesPlaceholder = document.getElementById("result");
            primesPlaceholder.innerText = result.join(", ");
        }, function (error) {
            var primesPlaceholder = document.getElementById("result");
            primesPlaceholder.innerText = error;
        });
    });
}

var saveFileRange = function (primesCalculator, localFolder, fileName, valueFrom, valueTo) {
    localFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.failIfExists).then(function (createdFile) {
        primesCalculator.calculatePrimesRange(valueFrom, valueTo).then(function (result) {
            Windows.Storage.FileIO.writeTextAsync(createdFile, result.join(", "));

            var primesPlaceholder = document.getElementById("result");
            primesPlaceholder.innerText = result.join(", ");
        }, function (error) {
            var primesPlaceholder = document.getElementById("result");
            primesPlaceholder.innerText = error;
        });
    });
}